﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLZJFurnitureSCM.BLL;
using QLZJFurnitureSCM.Model;

namespace QLZJFurnitureSCM.WinForm
{
    public partial class FrmAccountMgr : Form
    {
        private UserService userService = null;
        private List<User> lstUser = null;

        public FrmAccountMgr()
        {
            InitializeComponent();
            userService = new UserService();
            lstUser = userService.FindAll();
            foreach (User u in lstUser)
            {
                dataGridViewAccounts.Rows.Add(new object[] { u.UserId, u.UserName, u.UserPwd, (u.UserRole == 0) ? "会计" : "管理员", u.NickName });
            }
            cboUserRole.Items.Add("会计");
            cboUserRole.Items.Add("管理员");
            cboUserRole.SelectedIndex = 0;
            cboUserRole.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private void btnAccountAdd_Click(object sender, EventArgs e)
        {
            string userName = this.txtUserName.Text.Trim();
            string userPwd = this.txtUserPwd.Text;
            if (!this.txtPwdConfirm.Text.Equals(userPwd))
            {
                MessageBox.Show("两次输入的密码不一致, 请修改.", "错误");
                this.txtPwdConfirm.Focus();
                return;
            }
            int userRole = cboUserRole.SelectedIndex;
            string nickName = this.txtNickName.Text.Trim();
            if(string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(userPwd) || string.IsNullOrEmpty(nickName))
            {
                MessageBox.Show("请正确完整填写信息", "错误");
                return;
            }
            User user = new User();
            user.UserName = userName;
            user.UserPwd = userPwd;
            user.UserRole = userRole;
            user.NickName = nickName;
            if((from u in lstUser where u.UserName == user.UserName || u.NickName == user.NickName select u).Count()>0)
            {
                MessageBox.Show("帐号或昵称已存在, 请修改再添加.");
                return;
            }
            if (userService.Insert(user).UserId > 0)
            {
                dataGridViewAccounts.Rows.Add(new object[] { user.UserId, user.UserName, user.UserPwd, (user.UserRole == 0) ? "会计" : "管理员", user.NickName });
                MessageBox.Show("添加成功.");
            }
            else
                MessageBox.Show("添加失败.");
        }

        private void dataGridViewAccounts_Paint(object sender, PaintEventArgs e)
        {
            for (int i = 0; i < this.dataGridViewAccounts.Rows.Count; )
            {
                this.dataGridViewAccounts.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(86, 172, 210);
                i += 2;
            }
        }

        private void FrmAccountMgr_Load(object sender, EventArgs e)
        {
            
        }

        private void dataGridViewAccounts_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.ColumnIndex == this.dataGridViewAccounts.Columns["dgvColDelete"].Index)
            {
                int uid = int.Parse(dataGridViewAccounts["dgvColUserId", e.RowIndex].Value.ToString());
                if (uid == FrmMain.user.UserId)
                {
                    MessageBox.Show("不能删除自己.", "错误", MessageBoxButtons.OK);
                    return;
                }else
                {
                    if(DialogResult.Yes == MessageBox.Show("确认删除此帐号?", "提示", MessageBoxButtons.YesNo))
                    {
                        //remove current select user;
                        User user = new User();
                        user.UserId = uid;
                        if (userService.Delete(user))
                        {
                            this.dataGridViewAccounts.Rows.RemoveAt(e.RowIndex);
                            MessageBox.Show("删除成功.");
                        }
                        else
                            MessageBox.Show("删除失败, 请重启程序再尝试.");
                    }
                }
            }
        }
    }
}
